package com.budwk.app.wxprogram.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.wxprogram.models.vx_volunteer;
import com.budwk.app.wxprogram.services.VxVolunteerService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/wxprogram/volunteer")
@ApiDefinition(tag = "vx_volunteer")
@Slf4j
public class VxVolunteerController{
    @Inject
    private VxVolunteerService wxVolunteerService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询vx_volunteer")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = vx_volunteer.class
    )
    @ApiResponses(
    implementation = vx_volunteer.class
    )
    @SaCheckPermission("wxprogram.volunteer")
    public Result<?> list(@Param("..")vx_volunteer bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }

        if (bean.getName() !=null && Strings.isNotBlank(bean.getName().toString())) {
            cnd.and("name","like","%"+bean.getName()+"%");
        }

        if (bean.getSexual() !=null && Strings.isNotBlank(bean.getSexual().toString())) {
            cnd.and("sexual","like","%"+bean.getSexual()+"%");
        }

        if (bean.getPhone() !=null && Strings.isNotBlank(bean.getPhone().toString())) {
            cnd.and("phone","like","%"+bean.getPhone()+"%");
        }

        if (bean.getVxprovinces() !=null && Strings.isNotBlank(bean.getVxprovinces().toString())) {
            cnd.and("vxprovinces","like","%"+bean.getVxprovinces()+"%");
        }

        if (bean.getVxcities() !=null && Strings.isNotBlank(bean.getVxcities().toString())) {
            cnd.and("vxcities","like","%"+bean.getVxcities()+"%");
        }

        if (bean.getYears() !=null && Strings.isNotBlank(bean.getYears().toString())) {
            cnd.and("years","like","%"+bean.getYears()+"%");
        }

        if (bean.getProvinces() !=null && Strings.isNotBlank(bean.getProvinces().toString())) {
            cnd.and("provinces","like","%"+bean.getProvinces()+"%");
        }

        if (bean.getCities() !=null && Strings.isNotBlank(bean.getCities().toString())) {
            cnd.and("cities","like","%"+bean.getCities()+"%");
        }

        if (bean.getDistricts() !=null && Strings.isNotBlank(bean.getDistricts().toString())) {
            cnd.and("districts","like","%"+bean.getDistricts()+"%");
        }

        if (bean.getTownships() !=null && Strings.isNotBlank(bean.getTownships().toString())) {
            cnd.and("townships","like","%"+bean.getTownships()+"%");
        }

        if (bean.getServicetype() !=null && Strings.isNotBlank(bean.getServicetype().toString())) {
            cnd.and("servicetype","like","%"+bean.getServicetype()+"%");
        }

        if (bean.getServicetime() !=null && Strings.isNotBlank(bean.getServicetime().toString())) {
            cnd.and("servicetime","like","%"+bean.getServicetime()+"%");
        }

        if (bean.getRegistrationtime() !=null && Strings.isNotBlank(bean.getRegistrationtime().toString())) {
            cnd.and("registrationtime","like","%"+bean.getRegistrationtime()+"%");
        }
        
        return Result.data(wxVolunteerService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增vx_volunteer")
    @ApiFormParams(
        implementation = vx_volunteer.class
    )
    @ApiResponses
    @SLog(tag = "新增vx_volunteer", value = "${args[0].${table.pkName}}")
    @SaCheckPermission("wxprogram.volunteer.create")
    public Result<?> create(@Param("..")vx_volunteer wxVolunteer, HttpServletRequest req) {
        wxVolunteer.setCreatedBy(SecurityUtil.getUserId());
        wxVolunteerService.insert(wxVolunteer);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改vx_volunteer")
    @ApiFormParams(
        implementation = vx_volunteer.class
    )
    @ApiResponses
    @SLog(tag = "修改vx_volunteer", value = "${args[0].${table.pkName}}")
    @SaCheckPermission("wxprogram.volunteer.update")
    public Result<?> update(@Param("..")vx_volunteer wxVolunteer, HttpServletRequest req) {
        wxVolunteer.setUpdatedBy(SecurityUtil.getUserId());
        wxVolunteerService.updateIgnoreNull(wxVolunteer);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取vx_volunteer")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("wxprogram.volunteer")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        vx_volunteer wxVolunteer = wxVolunteerService.fetch(id);
        if (wxVolunteer == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wxVolunteer);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除vx_volunteer")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除vx_volunteer", value = "${args[0]}")
    @SaCheckPermission("wxprogram.volunteer.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        vx_volunteer wxVolunteer = wxVolunteerService.fetch(id);
        if (wxVolunteer == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxVolunteerService.delete(id);
        req.setAttribute("_slog_msg", wxVolunteer.getId());
        return Result.success();
    }
}
